package view
{
	import flash.events.Event;
	
	import model.DataProxy;
	import model.ViewProxy;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	

	public class ContentViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String 				= "ContentViewMediator";
		
		private var contentView:ContentView;
		private var contentViewData:Array;
		
		private var currentMediatorName:String;
		
		public function ContentViewMediator( viewComponent:Object = null )
		{
			super( NAME, viewComponent );
		}
		
		override public function onRegister():void
		{
			contentViewData = dataProxy.vo.contentViewData;
			
			contentView = new ContentView();
			contentView.addEventListener( ContentView.CHANGED, contentViewChangedHandler );
			contentView.init( dataProxy.vo.contentViewData, viewProxy.vo );
			
			viewProxy.vo.contentView = contentView; 
			
			viewComponent.addChild( contentView );
			
		}
		
		override public function listNotificationInterests():Array
		{
			return [ MenuView.CHANGED, ListView.EXPANDED, ContentView.SHOW ];
		}
		
		override public function handleNotification( notification:INotification ):void
		{
			var name:String = notification.getName();
			var body:Object = notification.getBody();
			
			switch( name )
			{
				case MenuView.CHANGED:
					updateContent( body.type );
					contentView.show();
					break;
				case ContentView.SHOW:
					updateContent( dataProxy.vo.currentViewData.type );
					contentView.show();
					break;
			}
		}
		
		private function updateContent( type:String ):void
		{
			if( currentMediatorName != "" ) facade.removeMediator( currentMediatorName );
			
			contentView.removeComponents();
			
			switch( type )
			{
				case "list":
					facade.registerMediator( new ListViewMediator() );
					currentMediatorName = ListViewMediator.NAME;
					viewProxy.addComponent( viewProxy.vo.listView );
					break;
				case "gallery":
					
					break;
				case "info":
					
					break;
				case "contactForm":
					
					break;
				case "blog":
					
					break;
				case "detail":
					
					break;
			}
		}
		
		private function contentViewChangedHandler( e:Event ):void
		{
			trace("changed");
		}
		
		public function get dataProxy():DataProxy
		{
			return facade.retrieveProxy( DataProxy.NAME ) as DataProxy;
		}
		
		public function get viewProxy():ViewProxy
		{
			return facade.retrieveProxy( ViewProxy.NAME ) as ViewProxy;
		}
	}
}